/**
 * com.mantisframework.sys.service.impl
 *

 */

package com.mantisframework.sys.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import jodd.util.StringUtil;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.mantisframework.core.base.service.BaseService;
import com.mantisframework.core.page.PageBean;
import com.mantisframework.model.MSysDepartment;
import com.mantisframework.sys.dao.ISysDepartmentDao;
import com.mantisframework.sys.enumbean.DeptSearchFields;
import com.mantisframework.sys.service.ISysDepartmentService;

/**
 * 
 * 
 * 
 * @author 李奎和
 * 
 *         <p>
 *         时间：2013-1-13 下午06:58:20
 *         </p>
 * 
 *         <p>
 *         电子邮件：likh@hwttnet.com
 *         </p>
 */
@Service("sysDepartmentService")
public class SysDepartmentServiceImpl extends BaseService<MSysDepartment>
		implements ISysDepartmentService {
	
	private static Logger _log = Logger.getLogger(SysDepartmentServiceImpl.class);
	
	@Resource
	private ISysDepartmentService sysDepartmentService;
	@Resource
	private ISysDepartmentDao sysDepartmentDao;

	@Override
	public MSysDepartment getPojoById(Serializable id) {
		return sysDepartmentDao.getById(MSysDepartment.class, id);
	}

	public boolean modifyUser(MSysDepartment dept) {
		return this.sysDepartmentDao.modify(dept);
	}

	public boolean deleteDeptById(Serializable id) {
		return this.sysDepartmentDao.delete(getPojoById(id));
	}

	public PageBean querySysDepts(Map<DeptSearchFields, String> criterias,
			PageBean pageBean, String orderDirect, String orderField) {
		int count = 0;
		StringBuffer buffer = new StringBuffer(" ");
		for(DeptSearchFields field : DeptSearchFields.values()) {
			switch (field) {
			case deptName:
				if (criterias.containsKey(field)) {
					buffer.append(count == 0 ? " where" : " and")
							.append(" deptName like '%")
							.append(criterias.get(field)).append("%'");
					count++;
				}
				break;
			case deptType:
				if (criterias.containsKey(field)) {
					buffer.append(count == 0 ? " where" : " and")
							.append(" deptType like '%")
							.append(criterias.get(field)).append("%'");
					count++;
				}
				break;
			default:
				break;
			}
		}
		
		if(StringUtil.isNotEmpty(orderField)&&StringUtil.isNotBlank(orderField)) {
			buffer.append(" order by ").append(orderField);
			if(StringUtil.isNotEmpty(orderDirect)&&StringUtil.isNotBlank(orderDirect)) {
				buffer.append(" ").append(orderDirect).append(" ");
			} else {
				buffer.append(" asc ");
			}
		} else {
			buffer.append(" order by deptOrd asc");
		}
		
		return sysDepartmentService.getPojoByCondition(new MSysDepartment(), buffer.toString(), pageBean);
	}
	
	/**
	 * @param sysDepartmentService 要设置的 sysDepartmentService
	 */
	
	public void setSysDepartmentService(ISysDepartmentService sysDepartmentService) {
		this.sysDepartmentService = sysDepartmentService;
	}

	/**
	 * @param sysDepartmentDao 要设置的 sysDepartmentDao
	 */
	
	public void setSysDepartmentDao(ISysDepartmentDao sysDepartmentDao) {
		this.sysDepartmentDao = sysDepartmentDao;
	}

}
